﻿using HRIS.Browser.UI.ConfigReader;
using HRIS.Browser.UI.Framework;
using HRIS.Browser.UI.Framework.DesktopWebControls;
using Iit.Automation.Browsers;
using Iit.Automation.Browsers.ConfigDataProvider;
using Iit.Automation.Browsers.Shared;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Remote;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HRIS.Browser.Resources
{
    public class ResourceManager
    {
        private static Dictionary<IitEnums.RegisteredBrowsers, ResourceManager> _selfInstances;
        private static ConfigProvider _configProvider;

        private IitEnums.RegisteredBrowsers _whichBrowser;
        private IBrowser _browser;
        private DataProvider _browserConfigProvider;
        private IComponentFactory _componentFactory;

        public IBrowser Browser
        {
            get
            {
                return _browser;
            }
        }

        public ConfigProvider ConfigProvider
        {
            get { return _configProvider; }
        }

        private ResourceManager(IitEnums.RegisteredBrowsers whichBrowser, IitEnums.PlatFormTypes platform)
        {
            _whichBrowser = whichBrowser;

            if (_browserConfigProvider == null)
            {
                _browserConfigProvider = DataProvider.GetInstance(whichBrowser);
            }

            _browser = BrowserFactory.GetBrowser(whichBrowser, _browserConfigProvider.DriverFolderPath);
            _componentFactory = ComponentFactory.GetInstance(_browser, whichBrowser, platform);
            _browser.ComponentFactory = _componentFactory;
        }

        static ResourceManager()
        {
            _configProvider = ConfigProvider.GetInstance();
            _selfInstances = new Dictionary<IitEnums.RegisteredBrowsers, ResourceManager>();
            ComponentFactory.Initialize(GetTypeLibrary());
        }

        private static Dictionary<Type, Dictionary<IitEnums.Environment, Type>> GetTypeLibrary()
        {
            Dictionary<Type, Dictionary<IitEnums.Environment, Type>> library = new Dictionary<Type, Dictionary<IitEnums.Environment, Type>>();
            library.Add(typeof(ILogin), new Dictionary<IitEnums.Environment, Type>());
            library[typeof(ILogin)].Add(IitEnums.Environment.DesktopChrome, typeof(Login));
            library[typeof(ILogin)].Add(IitEnums.Environment.DesktopIE, typeof(Login));
            library[typeof(ILogin)].Add(IitEnums.Environment.DesktopFireFox, typeof(Login));

            library.Add(typeof(IMenuBase), new Dictionary<IitEnums.Environment, Type>());
            library[typeof(IMenuBase)].Add(IitEnums.Environment.DesktopChrome, typeof(TopMenu));
            library[typeof(IMenuBase)].Add(IitEnums.Environment.DesktopIE, typeof(TopMenu));
            library[typeof(IMenuBase)].Add(IitEnums.Environment.DesktopFireFox, typeof(TopMenu));

            return library;
        }

        public static ResourceManager GetInstance(IitEnums.RegisteredBrowsers whichBrowser, IitEnums.PlatFormTypes platform)
        {
            if (!_selfInstances.ContainsKey(whichBrowser))
            {
                _selfInstances.Add(whichBrowser, new ResourceManager(whichBrowser, platform));
            }

            return _selfInstances[whichBrowser];
        }
    }
}
